package com.seari.service.impl;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.seari.bean.AlarmCountLineMalfunction;
import com.seari.bean.AlarmCountMalfunction;
import com.seari.bean.AlarmMessageBean;
import com.seari.bean.AlarmQuery;
import com.seari.bean.AllLineRealAlarm;
import com.seari.bean.AllLineStationFault;
import com.seari.bean.HealthImportDevice;
import com.seari.bean.RedisDeploy;
import com.seari.mapper.AlarmMapper;
import com.seari.pojo.EventBasePojo;
import com.seari.pojo.QueryBase;
import com.seari.service.AlarmService;
import com.seari.utils.IData;
import com.seari.utils.PropertiesUtil;

@Service("alarmServive")
public class AlarmServiceImpl implements AlarmService {

	@Autowired
	private AlarmMapper alarmMapper;

	@Override
	public List<AlarmMessageBean> queryAlarmData() {
		// TODO Auto-generated method stub
		
		return alarmMapper.queryAlarmData();

	}

	@Override
	public List<AlarmMessageBean> queryAlarmDataCtOt() {
		return alarmMapper.queryAlarmDataCtOt();
	}

	public List<AlarmCountMalfunction> queryAlarmMalfunction(){

		return alarmMapper.queryAlarmMalfunction();
	}

	@Override
	public List<AlarmCountMalfunction> queryStationAlarmTop() {
		return alarmMapper.queryStationAlarmTop();
	}

	@Override
	public List<AlarmCountMalfunction> queryAlarmTop() {
		return alarmMapper.queryAlarmTop();
	}

	@Override
	public List<AlarmMessageBean> queryRealTimeAlarmData() {
		return alarmMapper.queryRealTimeAlarmData();
	}

	@Override
	public List<AlarmCountMalfunction> queryMalfunctionCnt() {
		return alarmMapper.queryMalfunctionCnt();
	}

	@Override
	public List<IData> queryMalfunctionRank() {
		
		return alarmMapper.queryMalfunctionRank();
	}



	/* (non-Javadoc)
	 * @see com.seari.service.AlarmService#queryDeviceSum()
	 * 
	 * 重要设备名称 和 设备总数
	 * 
	 */
	@Override
	public Map<String, Object> queryDeviceSum() {
		List<Map<String, Object>> queryDeviceSum = alarmMapper.queryDeviceSum();
		Map<String, Object> resultMap = new HashMap<String, Object>();
		for (Map<String, Object> map : queryDeviceSum) {
			String device = null;
			Object deviceSum = null;
			for (Map.Entry<String, Object> entry : map.entrySet()) {
				if ("device".equals(entry.getKey())) {
					device = (String) entry.getValue();
				} else if ("deviceSum".equals(entry.getKey())) {
					deviceSum = entry.getValue();
				}
			}
			resultMap.put(device, deviceSum);
		}
		return resultMap;
	}

	@Override
	public List<Integer> queryAlarmLineMalfunction() {
        AlarmCountLineMalfunction acmf = alarmMapper.queryAlarmLineMalfunction();
        List<Integer> list12 = new ArrayList<>();
        list12.add(acmf.getBaojingCnt());
        list12.add(acmf.getZongshuCnt());
        return list12;
    }

	@Override
	public Map<String, String> queryAlarmSystem() {
        List<AlarmCountMalfunction> acmfs = alarmMapper.queryAlarmSystemCnt();
        NumberFormat numberFormat = NumberFormat.getInstance();
        numberFormat.setMaximumFractionDigits(2);
        Integer total=0;
        String result ="";
        Map<String,String> map= new HashMap<>();
        for (AlarmCountMalfunction acmf: acmfs) {
            total += acmf.getCntAlarmSystem();
        }
        for (AlarmCountMalfunction acmf: acmfs) {
            result =  numberFormat.format((float)acmf.getCntAlarmSystem()/(float)total*100);
            map.put(acmf.getAlarmSystem(),result);
        }
        return map;
    }



	@Override
	public List<HealthImportDevice> queryHealthImportDevice(QueryBase qb) {
		// TODO Auto-generated method stub
		return alarmMapper.queryHealthImportDevice(qb);
	}

	@Override
	public Map<String, Object> queryDevicePro() {
		List<Map<String, Object>> queryDeviceSum = alarmMapper.queryDevicePro();
		Map<String, Object> resultMap = new HashMap<String, Object>();
		List<String> deviceNameList = new ArrayList<String>();
		List<String> riskIndexList = new ArrayList<String>();
		for (Map<String, Object> map : queryDeviceSum) {
			for (Map.Entry<String, Object> entry : map.entrySet()) {
				if ("deviceName".equals(entry.getKey())) {
					deviceNameList.add((String) entry.getValue());
				} else if ("riskIndex".equals(entry.getKey())) {
					riskIndexList.add(entry.getValue().toString());
				}
			}
		}
		resultMap.put("xData", deviceNameList);
		resultMap.put("yData", riskIndexList);
		return resultMap;
	}

	@Override
	public Map<String, Object> queryDeviceTopTen() {
		List<Map<String, Object>> queryDeviceTopTen = alarmMapper.queryDeviceTopTen();
		Map<String, Object> resultMap = new HashMap<String, Object>();
		List<String> deviceList = new ArrayList<String>();
		List<String> deviceFaultList = new ArrayList<String>();
		for (Map<String, Object> map : queryDeviceTopTen) {
			for (Map.Entry<String, Object> entry : map.entrySet()) {
				if ("device".equals(entry.getKey())) {
					deviceList.add((String) entry.getValue());
				} else if ("deviceFault".equals(entry.getKey())) {
					deviceFaultList.add(entry.getValue().toString());
				}
			}
		}
		resultMap.put("xData", deviceList);
		resultMap.put("yData", deviceFaultList);
		return resultMap;
	}

	@Override
	public List<AllLineStationFault> queryAllLineCntFault() {
		return alarmMapper.queryAllLineCntFault();
	}

	@Override
	public Map<String, Object> queryAllLineFault() throws Exception {
		List<Map<String, Object>> maps = alarmMapper.queryAllLineFault();
		Properties pt = PropertiesUtil.loadConfig("stationId.properties");
		Map<String,Object> resultMap=new HashMap<>();
		String station = null;
		Object alarmRank = null;
		for (Map<String, Object> map:maps){

			for (Map.Entry<String, Object>entry:map.entrySet()) {
				if ("station_cn".equals(entry.getKey())){
					station=String.valueOf(pt.get(entry.getValue()));
				}else if ("alarm_rank".equals(entry.getKey())){
					alarmRank = entry.getValue();
				}

			}
			resultMap.put(station,alarmRank);
		}

		return resultMap;
	}

	@Override
	public List<String> queryDevicePort() {
        List<Map<String, String>> maps = alarmMapper.queryDevicePort();
        List<String> devices=new ArrayList<>();
        for (Map<String, String> map: maps) {
            for (Map.Entry<String, String>entry:map.entrySet()) {
                devices.add(entry.getValue());
            }
        }
		 return devices;
	}

	@Override
	public int getHealthIndex() {
		
		int healthIndex = 100 - alarmMapper.getHealthIndex();
		
		if(healthIndex == 100){
			
			healthIndex = 99;
		}
		return healthIndex;
	}

	@Override
	public Map<String,Double> systemHealthValue() {
		Map<String,Double> nums = new HashMap<>();
		Integer allEventCount = alarmMapper.getAllEventCount();
		if (allEventCount == null||allEventCount==0) {
			allEventCount=1;
		}
		List<EventBasePojo> maxEvenCount = alarmMapper.getMaxEvenCount();
		for (EventBasePojo item:maxEvenCount){
			double v = Double.valueOf(item.getAlarmCount()) / allEventCount;
			String format = String.format("%.2f", v);
			Double res = (1-Double.valueOf(format))*100;
			nums.put(item.getAlarmSystem(),res);
		}
		return nums;
	}

	@Override
	public int getDeviceHealthCount() {
		// TODO Auto-generated method stub
		return alarmMapper.getDeviceHealthCount();
	}

	@Override
	public List<RedisDeploy> getRedisDeploys(String redisName) {
		// TODO Auto-generated method stub
		return alarmMapper.getRedisDeploys(redisName);
	}

	@Override
	public List<AllLineRealAlarm> getAllLineRealAlarm(AlarmQuery alarmQuery) {
		// TODO Auto-generated method stub
		return alarmMapper.getAllLineRealAlarm(alarmQuery);
	}

	@Override
	public int getAllLineRealAlarmCount(AlarmQuery alarmQuery) {
		// TODO Auto-generated method stub
		return alarmMapper.getAllLineRealAlarmCount(alarmQuery);
	}
}